package com.wisdytech.linkdcs.linkdcsservicepad.pad.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.app.quality.dao.IssueAttachmentDao;
import com.wisdytech.app.quality.dao.IssueDao;
import com.wisdytech.app.quality.vo.EquipmentVO;
import com.wisdytech.app.quality.vo.ManufactureVO;
import com.wisdytech.app.quality.vo.OperationVO;
import com.wisdytech.app.quality.vo.SysUserVO;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.linkdcsservicepad.common.CommonConstants;
import com.wisdytech.linkdcs.linkdcsservicepad.common.exception.BaseException;
import com.wisdytech.linkdcs.linkdcsservicepad.common.handler.BaseService;
import com.wisdytech.linkdcs.linkdcsservicepad.pad.service.IssueService;
import com.wisdytech.linkdcs.pad.model.Issue;
import com.wisdytech.linkdcs.pad.model.IssueAttachment;
import com.wisdytech.linkdcs.system.dao.ISysUserDao;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  品质异常服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-08-15
 */
@Service("issueService")
@Transactional
public class IssueServiceImpl extends BaseService<IssueDao, Issue> implements IssueService {

    /**
     * 注入异常登记dao
     */
    @Resource
    private IssueDao issueDao;

    /**
     * 注入异常信息附件dao
     */
    @Resource
    private IssueAttachmentDao issueAttachmentDao;

    /**
     * 注入设备dao
     */
    @Resource
    private IEqEquipmentDao equipDao;

    /**
     * 注入用户dao
     */
    @Resource
    private ISysUserDao iSysUserDao;

    @Override
    public List<Issue> getIssueRecord(Filter filter) {
        EntityWrapper<Issue> ew = new EntityWrapper<>();
        ew.eq("ISSUE_STATUS",CommonConstants.ISSUE_STATUS_REPORTED);
        ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
        List<Issue> list = super.beforeList(IssueDao.class,filter).selectList(ew);
        return list;
    }

    @Override
    public List<Issue> getIssueRecord(String issueStatus, Filter filter) {
        EntityWrapper<Issue> ew = new EntityWrapper<>();
        if(StringUtils.isNotEmpty(issueStatus)){
            ew.eq("ISSUE_STATUS",issueStatus);
        }
        ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
        List<Issue> list = super.beforeList(IssueDao.class,filter).selectList(ew);
        return list;
    }

    @Override
    public void saveIssueRecord(Issue issue, IssueAttachment issueAttachment, String username) {
        //插入异常登记信息
        issue.setDelFlag(CommonConstants.DEL_FLAG);
        issue.setCreateUser(username);
        issue.setCloseDate(new Date());
        issue.setIssueStatus(CommonConstants.ISSUE_STATUS_REPORTED);
        int count = issueDao.insert(issue);
        if(count<0){
            throw new BaseException("登记失败");
        }
        //插入附近信息
        issueAttachment.setCreateDate(new Date());
        issueAttachment.setCreateUser(username);
        issueAttachment.setDelFlag(CommonConstants.DEL_FLAG);
        int count1 = issueAttachmentDao.insert(issueAttachment);
        if(count1<0){
            throw new BaseException("新增附件失败");
        }
    }

    @Override
    public void analyseIssue(Issue issue, String username) {
        Issue issueRecord = this.issueDao.selectById(issue.getPkId());
        issueRecord.setAnalyseDate(new Date());
        issueRecord.setAnalyseReason(issue.getAnalyseReason());
        issueRecord.setAnalyseUser(username);
        issueRecord.setIssueStatus(CommonConstants.ISSUE_STATUS_ANALYSE);
        this.updateCommon(issueRecord,username);
        this.issueDao.updateById(issueRecord);
    }

    @Override
    public void reviewIssue(Issue issue, String username) {
        Issue issueRecord = this.issueDao.selectById(issue.getPkId());
        issueRecord.setReviewDate(new Date());
        issueRecord.setReportUser(username);
        issueRecord.setReviewDesc(issue.getReviewDesc());
        issueRecord.setReviewRole(issue.getReviewRole());
        issueRecord.setIssueStatus(CommonConstants.ISSUE_STATUS_REVIEW);
        this.updateCommon(issueRecord,username);
        this.issueDao.updateById(issueRecord);
    }

    @Override
    public void decideIssue(Issue issue, String username) {
        Issue issueRecord = this.issueDao.selectById(issue.getPkId());
        issueRecord.setDecideDate(new Date());
        issueRecord.setDecideUser(username);
        issueRecord.setDecideDesc(issue.getDecideDesc());
        issueRecord.setIssueStatus(CommonConstants.ISSUE_STATUS_DECIDE);
        this.updateCommon(issueRecord,username);
        this.issueDao.updateById(issueRecord);
    }

    @Override
    public void solveIssue(Issue issue, String username) {
        Issue issueRecord = this.issueDao.selectById(issue.getPkId());
        issueRecord.setSolveDate(new Date());
        issueRecord.setSolveDesc(issue.getIssueDesc());
        issueRecord.setSolveUser(username);
        issueRecord.setPreventDesc(issue.getPreventDesc());
        issueRecord.setIssueStatus(CommonConstants.ISSUE_STATUS_SOLVE);
        this.updateCommon(issueRecord,username);
        this.issueDao.updateById(issueRecord);
    }

    @Override
    public void confirmIssue(Issue issue, String username) {
        Issue issueRecord = this.issueDao.selectById(issue.getPkId());
        issueRecord.setConfirmDate(new Date());
        issueRecord.setConfirmUser(username);
        issueRecord.setConfirmResult(issue.getConfirmResult());
        issueRecord.setConfirmDesc(issue.getConfirmDesc());
        issueRecord.setIssueStatus(CommonConstants.ISSUE_STATUS_CONFIRM);
        this.updateCommon(issueRecord,username);
        this.issueDao.updateById(issueRecord);
    }

    @Override
    public void closeIssue(String issueId, String username) {
        Issue issueRecord = this.issueDao.selectById(issueId);
        issueRecord.setCloseDate(new Date());
        issueRecord.setCloseUser(username);
        issueRecord.setIssueStatus(CommonConstants.ISSUE_STATUS_CLOSE);
        this.updateCommon(issueRecord,username);
        this.issueDao.updateById(issueRecord);
    }

    @Override
    public ManufactureVO getManufactureByCode(String manufactureCode) {
        return issueDao.getManufactureByCode(manufactureCode);
    }

    @Override
    public List<OperationVO> getOperationBylineId(String lineId) {
        return this.issueDao.getOperationBylineId(lineId);
    }

    @Override
    public List<EquipmentVO> getEquipByLineIdAndOpId(String lineId, String opId) {
        EntityWrapper<Equipment> ew = new EntityWrapper();
        ew.eq("LINE_ID",lineId);
        ew.eq("OPERATION_ID",opId);
        ew.eq("DEL_FLAG",CommonConstants.DEL_FLAG);
        List<Equipment> list = this.equipDao.selectList(ew);
        List<EquipmentVO> listVO = new ArrayList<>();
        for(Equipment eq : list){
            EquipmentVO vo = new EquipmentVO();
            vo.setEquipId(eq.getPkId());
            vo.setEquipCode(eq.getEquipCode());
            listVO.add(vo);
        }
        return listVO;
    }

    @Override
    public List<SysUserVO> getUser() {
        EntityWrapper<SysUser> ew = new EntityWrapper();
        ew.eq("DEL_FLAG",CommonConstants.DEL_FLAG);
        List<SysUser> list = this.iSysUserDao.selectList(ew);
        List<SysUserVO> listVO = new ArrayList<>();
        for (SysUser user : list){
            SysUserVO vo = new SysUserVO();
            vo.setUserId(user.getPkId());
            vo.setTruename(user.getTruename());
            listVO.add(vo);
        }
        return listVO;
    }

    private void updateCommon(Issue issueRecord, String username) {
        issueRecord.setUpdateDate(new Date());
        issueRecord.setUpdateUser(username);
    }
}
